package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.cg.dto.AddCommunityDto;
import com.ruoyi.common.core.cg.dto.CommunityDto;
import com.ruoyi.common.core.cg.pojo.Community;
import com.ruoyi.common.core.cg.pojo.Leader;
import com.ruoyi.common.core.cg.pojo.Location;
import com.ruoyi.common.core.cg.pojo.QQMapCommunity;
import com.ruoyi.common.core.cg.result.Result;
import com.ruoyi.user.mapper.CommunityMapper;
import com.ruoyi.user.service.CommunityService;
import com.ruoyi.user.service.LocationService;
import com.ruoyi.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommunityServiceImpl implements CommunityService {
    @Autowired
    private CommunityMapper mapper;
    @Autowired
    private LocationService locationService;
    @Autowired
    private UserService userService;
    /**
     * 查询用户对应的社区
     * @param id
     * @return
     */
    @Override
    public Community findByUserId(Integer id) {
        // 查询用户对应的社区
        Community community = mapper.findByUserId(id);
        if (community == null){
            return null;
        }
        // 查询社区对应的团长
        Leader leader = mapper.findLeaderById(community.getId());
        if (leader != null){
            community.setLeader(leader);
        }else {
            Leader defaultLeader = new Leader();
            defaultLeader.setName("暂无");
            community.setLeader(defaultLeader);
        }
        // 查询当前社区对应的用户人数
        Integer member = mapper.findMemberById(community.getId());
        community.setMember(member);
        return community;
    }

    /**
     * 查询团所对应的社区
     * @param id
     * @return
     */
    @Override
    public Community findCommunityByGroup(Integer id) {
        return mapper.findCommunityByGroup(id);
    }
    // 通过QQmapId查询拼团点
    @Override
    public Community findCommunityByQQmapId(String qqMapId) {
        return mapper.findCommunityByQQmapId(qqMapId);
    }

    /**
     * 修改拼团点人数
     * @param qqMapId
     */
    @Override
    public void updateCommunityMember(String qqMapId) {
        mapper.updateCommunityMember(qqMapId);
    }

    /**
     * 切换用户拼团点
     * @param dto
     */
    @Override
    public void changeUserCommunity(AddCommunityDto dto) {
        // 首先查询拼团点是否存在
        Community community = mapper.findCommunityByQQmapId(dto.getQqMapId());
        System.err.println("查询到的"+dto.getQqMapId());
        // 查询原来用户所属拼团点
        Community originalCommunity = mapper.findByUserId(dto.getUserId());
        // 如果存在
        if (null != community){
            // 退出原来所属拼团点
            mapper.quitCommunity(dto.getUserId(),originalCommunity.getId());
            mapper.reduceCommunityMember(dto.getId());
            // 加入新的拼团点
            mapper.updateCommunityMember(dto.getQqMapId());
            mapper.addUserCommunity(dto.getUserId(),community.getId());
        }else {
            // 不存在先添加拼团点
            Community newCommunity = new Community();
            newCommunity.setQqMapId(dto.getQqMapId());
            newCommunity.setTitle(dto.getTitle());
            newCommunity.setAddress(dto.getAddress());
            newCommunity.setLat(dto.getLat());
            newCommunity.setLng(dto.getLng());
            // 退出原来所属拼团点
            mapper.quitCommunity(dto.getUserId(),originalCommunity.getId());
            mapper.reduceCommunityMember(dto.getId());
            // 进入拼团点
            mapper.addCommunity(newCommunity);
            mapper.addUserCommunity(dto.getUserId(),newCommunity.getId());
        }
    }

    // 添加拼团点
    @Override
    public void addCommunity(Community newCommunity) {
        mapper.addCommunity(newCommunity);
    }

    /**
     * 查询社区对应的团长
     * @param id
     * @return
     */
    @Override
    public Leader findLeaderById(Integer id) {
        return mapper.findLeaderById(id);
    }

    /**
     * 批量插入社区
     * @param dto
     */
    @Override
    public void batchAddCommunity(CommunityDto dto) {
        Location location = dto.getLocation();
        List<QQMapCommunity> qqMapCommunityList = dto.getNearbyCommunities();
        // 判断是否插入过数据
        Integer exists = locationService.findLocation(location);
        List<Community> communityList = new ArrayList<>();
        if (exists == null){
            for (QQMapCommunity qqMapCommunity:qqMapCommunityList){
                Community community = new Community();
//                community.setQqMapId(qqMapCommunity.getId());
                community.setTitle(qqMapCommunity.getTitle());
                community.setAddress(qqMapCommunity.getAddress());
                communityList.add(community);
            }
            // 添加查询过的坐标
            locationService.addLocation(location);
            // 批量插入社区
            mapper.batchAddCommunity(communityList);
        }

    }

    @Override
    public Result findCommunityInfo(CommunityDto dto) {
        List<QQMapCommunity> qqMapCommunities = dto.getNearbyCommunities();
        List<Community> communityList = new ArrayList<>();
        for (QQMapCommunity qqMapCommunity:qqMapCommunities){
            Community community = new Community();
            community.setTitle(qqMapCommunity.getTitle());
            community.setQqMapId(qqMapCommunity.getId());
            community.setAddress(qqMapCommunity.getAddress());
            community.set_distance(qqMapCommunity.get_distance());
            community.setLat(qqMapCommunity.getLocation().get("lat"));
            community.setLng(qqMapCommunity.getLocation().get("lng"));
            Community communityByQQmapId = mapper.findCommunityByQQmapId(qqMapCommunity.getId());
            if (null != communityByQQmapId){
                Integer member = mapper.findMemberById(communityByQQmapId.getId());
                community.setMember(member);
                Leader leader = mapper.findLeaderById(communityByQQmapId.getId());
                community.setLeader(leader);
            }
            communityList.add(community);
        }
        return new Result(true,"查询成功",communityList);

//        List<String> qqMapId = new ArrayList<>();
//        for (QQMapCommunity qqMapCommunity:qqMapCommunities){
//            qqMapId.add(qqMapCommunity.getId());
//        }
//        // 查询表中存在的拼团点
//        List<Community> communityList = mapper.findCommunityInfo(qqMapId);
//        for (Community community:communityList){
//            community.setMember(mapper.findMemberById(community.getId()));
//        }
//        return new Result(true,"查询成功",communityList);
    }


}
